{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Worker Pools"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we’ll look at how to implement a worker pool using goroutines and channels.\n",
    "\n",
    "Comments from [Go by Example](https://gobyexample.com/worker-pools)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    "    \"time\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Worker Function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here’s the worker, of which we’ll run several concurrent instances. These workers will receive work on the jobs channel and send the corresponding results on results. We’ll sleep a second per job to simulate an expensive task."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "func worker(id int, jobs <-chan int, results chan<- int) {\n",
    "    for j := range jobs {\n",
    "        fmt.Println(\"worker\", id, \"processing job\", j)\n",
    "        time.Sleep(time.Second)\n",
    "        results <- j * 2\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Utilize Workers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to use our pool of workers we need to send them work and collect their results. We make 2 channels for this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "jobs := make(chan int, 100)\n",
    "results := make(chan int, 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, start up 3 workers, initially blocked because there are no jobs yet:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for w := 1; w <= 3; w++ {\n",
    "    go worker(w, jobs, results)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Send 9 jobs and then close that channel to indicate that’s all the work we have:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "worker 4 processing job 1\n",
       "worker 4 processing job 2\n",
       "worker 4 processing job 3\n",
       "worker 4 processing job 4\n",
       "worker 4 processing job 5\n",
       "worker 4 processing job 6\n",
       "worker 4 processing job 7\n",
       "worker 4 processing job 8\n",
       "worker 4 processing job 9\n"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for j := 1; j <= 9; j++ {\n",
    "    jobs <- j\n",
    "}\n",
    "time.Sleep(3*time.Second)\n",
    "close(jobs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go",
   "language": "go",
   "name": "gophernotes"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
